package com.dream.inspect.modules.business.service.impl;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.dream.inspect.common.utils.PageUtils;
import com.dream.inspect.common.utils.Query;
import com.dream.inspect.common.utils.R;
import com.dream.inspect.modules.business.dao.InstructionsTargetDao;
import com.dream.inspect.modules.business.dao.SysMessageDao;
import com.dream.inspect.modules.business.dao.SysMessageTargetDao;
import com.dream.inspect.modules.business.entity.InstructionsTargetEntity;
import com.dream.inspect.modules.business.entity.SysMessageEntity;
import com.dream.inspect.modules.business.entity.SysMessageTargetEntity;
import com.dream.inspect.modules.business.service.SysMessageService;
import com.dream.inspect.modules.dream.pojo.SysMessageFormMap;
import com.dream.inspect.modules.sys.dao.SysRoleDeptDao;


@Service("sysMessageService")
public class SysMessageServiceImpl extends ServiceImpl<SysMessageDao, SysMessageEntity> implements SysMessageService {

	@Resource
	private SysMessageDao sysMessageDao;
	@Resource
	private SysMessageTargetDao sysMessageTargetDao;
	@Resource
	private InstructionsTargetDao instructionsTargetDao;
	@Resource
	private SysRoleDeptDao sysRoleDeptDao;
	
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<SysMessageEntity> page = this.selectPage(
                new Query<SysMessageEntity>(params).getPage(),
                new EntityWrapper<SysMessageEntity>()
        );

        return new PageUtils(page);
    }

	@Override
	public PageUtils findMessageListByApp(Map<String, Object> params) {
		Page<SysMessageFormMap> page = new Query<SysMessageFormMap>(params).getPage();
		page.setRecords(sysMessageDao.findMessageListByApp(params,page));
		return new PageUtils(page);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public R read(Map<String, Object> params) {
		String typeFlag = params.get("typeFlag").toString();
		switch(typeFlag){
			case "1" : 
				SysMessageTargetEntity smt = new SysMessageTargetEntity();
				smt.setReadFlag("1");
				smt.setDeleteFlag("0");
				smt.setNoticeId(Long.parseLong(params.get("noticeId").toString()));
				smt.setUserId(Long.parseLong(params.get("userId").toString()));
				smt.setDeptId(Long.parseLong(params.get("deptId").toString()));
				smt.setCreateTime(new Date());
				smt.setType("1");
				sysMessageTargetDao.insert(smt);
				return R.ok().put("data", "success");
			case "2" : 
				Long targetId = Long.parseLong(params.get("targetId").toString());
				InstructionsTargetEntity it = new InstructionsTargetEntity();
				it.setId(targetId);
				it.setReadFlag("1");
				it.setUpdateTime(new Date());
				instructionsTargetDao.updateById(it);
				return R.ok().put("data", "success");
			case "3" : 
				/*SysMessageEntity sm = new SysMessageEntity();
				Long targetId1 = Long.parseLong(params.get("targetId").toString());
				sm.setId(targetId1);
				sm.setReadFlag("1");
				sm.setUpdateTime(new Date());
				sysMessageDao.updateById(sm);
				return R.ok().put("data", "success");*/
				SysMessageTargetEntity smt2 = new SysMessageTargetEntity();
				smt2.setReadFlag("1");
				smt2.setDeleteFlag("0");
//				smt2.setNoticeId(Long.parseLong(params.get("noticeId").toString()));
				smt2.setNoticeId(Long.parseLong(params.get("targetId").toString()));
				smt2.setUserId(Long.parseLong(params.get("userId").toString()));
				smt2.setDeptId(Long.parseLong(params.get("deptId").toString()));
				smt2.setCreateTime(new Date());
				smt2.setType("2");
				sysMessageTargetDao.insert(smt2);
				return R.ok().put("data", "success");
			default : return R.error("请输入类型标志(1:通知通报;2:指令;3:系统消息;)").put("data","1:通知通报;2:指令;3:系统消息;");
		}
	}
	
	/**
	 * 插入系统消息
	 */
	@Override
	public Long insertMessage(Map<String, String> params) {
		Long result = 0L;
		Date now = new Date();
		String noticeTitle = params.get("noticeTitle");  //消息标题
		String noticeContent = params.get("noticeContent");	//消息内容
		String userId = params.get("userId");	        
		String acceptUserId = params.get("acceptUserId");  //接收用户
		String deptId = params.get("deptId");	           //接收单位
		String roles = params.get("roles");	        
		
		SysMessageEntity smEntity = new SysMessageEntity();
		if (StringUtils.isNotEmpty(noticeTitle)) {
			smEntity.setNoticeTitle(noticeTitle);           //消息标题
        }
		if (StringUtils.isNotEmpty(noticeContent)) {
			smEntity.setNoticeContent(noticeContent);       //消息内容
        }
		if (StringUtils.isNotEmpty(acceptUserId)) {
			smEntity.setUserId(Long.valueOf(acceptUserId));       //接收用户
        }
		if (StringUtils.isNotEmpty(deptId)) {
			smEntity.setDeptId(Long.valueOf(deptId));       //接收用户
        }
		if (StringUtils.isNotEmpty(roles)) {
			smEntity.setRoles(roles);                       //接收角色
        }		
		smEntity.setCreateUser(Long.valueOf(userId));           //创建人id
		smEntity.setCreateTime(now);                            //创建时间
		smEntity.setUpdateUser(Long.valueOf(userId));           //更新人id
		smEntity.setUpdateTime(now);                            //更新时间
		
		this.insert(smEntity);
		result = smEntity.getId();
		return result;
	}

	@Override
	public PageUtils findSysMsg(Map<String, Object> params) {
		Page<SysMessageFormMap> page = new Query<SysMessageFormMap>(params).getPage();
		page.setRecords(sysMessageDao.findSysMsg(params,page));
		return new PageUtils(page);
	}

}
